package com.cn.modules.menu;

import java.util.ArrayList;
import java.util.List;

import com.cn.common.utils.StringUtil;
import com.cn.component.treeData.TreeNode;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * DROP TABLE IF EXISTS `menu`;
   CREATE TABLE `menu` (
  `CODE` varchar(32) NOT NULL,
  `NAME` varchar(50) DEFAULT NULL,
  `PARENT_ID` varchar(32) DEFAULT NULL,
  `PATH` varchar(200) DEFAULT NULL,
  `ICON_ID` varchar(32) DEFAULT NULL,
  `OPER_TIME` datetime DEFAULT '0000-00-00 00:00:00',
  `SORT_NUMBER` int(3) DEFAULT NULL,
  PRIMARY KEY (`CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 * @author Administrator
 *
 */
public class Menu extends Model<Menu>{

	private static final long serialVersionUID = 7235772949830383702L;

	public final static Menu dao=new Menu();

	/**
	 * 分页
	 * @param pageNumber
	 * @param pageSize
	 * @param sidx
	 * @param sord
	 * @return
	 */
	public Page<Menu> selectJqGridPage(int pageNumber, int pageSize, String sidx,String sord) {
		 pageNumber = pageNumber != 0 ? pageNumber: 1;
		 pageSize = pageSize != 0 ? pageSize : 10;
		sidx = StringUtil.isNull(sidx) ? " SORT_NUMBER" : sidx;
		sord = StringUtil.isNull(sord) ? " asc" : sord;
		return paginate(pageNumber, pageSize, "select *", "from MENU order by "+ sidx +"  "+ sord);
	}
	
	/**
	 * By ParentId
	 * @param parentId
	 * @return
	 */
	public List<Menu> findMenuyParentId(String parentId){
		return find("select * from MENU where PARENT_ID =? order by SORT_NUMBER",parentId);
	}
	
	/**
	 * find All
	 * @return
	 */
	public List<Menu> findAll(){
		return find("select * from MENU order by SORT_NUMBER");
	}
	
	/**
	 * 获取Menu转化TreeNode
	 * @return
	 */
	public List<TreeNode> getTreeNodeList(){
		TreeNode node = null;
		List<TreeNode> list = new ArrayList<TreeNode>();
		List<Menu> mList = this.findAll();
		for (Menu menu : mList) {
			node = new TreeNode();
			node.setId(menu.getStr("CODE"));
			node.setName(menu.getStr("NAME"));
			node.setParentId(menu.getStr("PARENT_ID"));
			node.setChecked(true);
			list.add(node);
		}
		return list;
	}
}
